local skel = fk.CreateSkill {
  name = "lb__moxi",
}
skel.visible = true

Fk:loadTranslationTable {
  ["lb__moxi"] = "殁息",
  --[":lb__moxi"] = "出牌阶段开始时，你可以减2点体力上限并视为对一名角色使用一张火【杀】，然后你可对其重复发动直至其进入濒死状态。若发动时你未受伤，你移除副将并可以为此【杀】增加一个目标。 ",
  --9.2
  --[":lb__moxi"] = "出牌阶段开始时，你可以减2点体力上限并视为对一名角色使用一张【杀】，然后你可对其重复发动直至其进入濒死状态。若发动时你未受伤，你移除副将并可以为此【杀】增加一个目标。 ",
  --9.23
  [":lb__moxi"] = "出牌阶段开始时，你可以减2点体力上限并视为对一名角色使用一张无距离限制的【杀】，然后你可对其重复发动直至其进入濒死状态。若发动时你未受伤，你移除副将并可以为此【杀】增加一个目标。 ",

  ["#lb__moxi"] = "殁息：你可以减2点体力上限并视为对一名角色使用一张【杀】",
  ["#lb__moxi-slash"] = "殁息：对至多 %arg名角色使用一张【杀】",
  ["#lb__moxi-loop"] = "殁息：你可以减2点体力上限并视为对 %dest 使用一张【杀】",
  ["#lb__moxi-extra"] = "殁息：你可以为【杀】增加一个目标",

  ["$lb__moxi1"] = "请就此凋零。",
  ["$lb__moxi2"] = "沉眠吧。",
  ["$lb__moxi3"] = "惟愿死亡…捍卫你我。",
}

skel:addEffect(fk.EventPhaseStart, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player and data.phase == Player.Play
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, { skill_name = skel.name, prompt = "#lb__moxi" })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player, skel.name, "offensive")
    room:changeMaxHp(player, -2)
    if player.dead then return end
    data.extra_data = data.extra_data or {}
    local default = {}
    if data.extra_data.lb__moxi then
      table.insert(default, room:getPlayerById(data.extra_data.lb__moxi))
    end
    local tos = table.filter(room:getOtherPlayers(player), function(t)
      return player:canUseTo(Fk:cloneCard("slash"), t, { bypass_times = true, bypass_distances = true })
    end)
    local n = 1
    local audio = math.random(1, 2)
    if not player:isWounded() and player.deputyGeneral ~= "" then
      audio = 3
      player:broadcastSkillInvoke(skel.name, 3)
      n = n + 1
      room:changeHero(player, "", false, true, true, false)
      if player.dead then return end
      if #default > 0 then
        table.removeOne(tos, default[1])
        if #tos > 0 then
          local t = room:askToChoosePlayers(player, {
            targets = tos,
            min_num = 1,
            max_num = 1,
            skill_name = skel.name,
            cancelable = true,
            prompt = "#lb__moxi-extra",
          })
          if #t > 0 then
            table.insert(default, t[1])
          end
        end
      end
    end
    if audio < 3 then
      player:broadcastSkillInvoke(skel.name, audio)
    end
    n = math.min(n, #tos)
    if #default > 0 or n > 0 then
      tos = #default > 0 and default or room:askToChoosePlayers(player, {
        targets = tos,
        min_num = 1,
        max_num = n,
        skill_name = skel.name,
        cancelable = false,
        prompt = "#lb__moxi-slash:::" .. n,
      })
      room:useVirtualCard("slash", nil, player, tos, skel.name, true)
      local t = tos[1]
      if #tos == 1 and not t.dead and t:getMark("lb__moxi-phase") == 0 and player:hasSkill(skel.name) and room:askToSkillInvoke(player, { skill_name = skel.name, prompt = "#lb__moxi-loop::" .. t.id }) then
        data.extra_data = data.extra_data or {}
        data.extra_data.lb__moxi = t.id
        room:useSkill(player, self, function()
          return self:use(event, target, player, data)
        end)
      end
      room:setPlayerMark(t, "lb__moxi-phase", 0)
    end
  end,
})

skel:addEffect(fk.EnterDying, {
  can_refresh = function(self, event, target, player, data)
    return data.damage and data.damage.card and data.damage.card.skillName == skel.name
  end,
  on_refresh = function(elf, event, target, player, data)
    target.room:setPlayerMark(target, "lb__moxi-phase", 1)
  end,
})

return skel
